package com.hspedu.method;


/**
 * @Author Stringzhua
 * @Date 2024/3/15 15:00
 * description:
 */
public class ThreadMethodExercise {
    public static void main(String[] args) throws InterruptedException {
        Thread t3 = new Thread(new T3());//创建子线程
        for (int i = 0; i <= 10; i++) {
            System.out.println("hi" + i);
            if (i == 5) {//说明主线程输出了五次hi
                t3.start();//启动子线程 输出hello
                t3.join();//立即将t3子线程插入到main让t3先执行
            }
            Thread.sleep(1000);
        }
        System.out.println("主线程结束");
    }
}

class T3 implements Runnable {
    private int count = 0;

    @Override
    public void run() {
        while (true) {
            System.out.println("hello" + (++count));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count == 10) {
                break;
            }
        }
        System.out.println("子线程结束");
    }
}
